package com.baor.service.impl;

import com.baor.dao.MarketplaceOverviewDao;
import com.baor.service.MarketplaceOverviewService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MarketplaceOverviewServiceImpl implements MarketplaceOverviewService {

    @Autowired
    private MarketplaceOverviewDao marketplaceOverviewDao;

    @Override
    public AjaxResult<?> overview(Map<String, Object> params) throws ParseException {
        Map<String,Object> res = new HashMap<>();
        res.put("ordersNumberToday",marketplaceOverviewDao.ordersNumber(params));
        res.put("salesToday",marketplaceOverviewDao.sales(params));
        res.put("refundToday",marketplaceOverviewDao.refund(params));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = (String) params.get("date");
        Date date = format.parse(dateString);
        long time = date.getTime();
        long day=86400000;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String time1=sdf.format(new Date(time-day));

        Map<String,Object> params2 = new HashMap<>();
        params2.put("date",time1);
        params2.put("siteId",params.get("siteId"));
        res.put("ordersNumberYesterday",marketplaceOverviewDao.ordersNumber(params2));
        res.put("salesYesterday",marketplaceOverviewDao.sales(params2));
        res.put("refundYesterday",marketplaceOverviewDao.refund(params2));
        Map<String,Object> params3 = new HashMap<>();
        params3.put("siteId",params.get("siteId"));
        res.put("ordersNumber",marketplaceOverviewDao.ordersNumber(params3));
        res.put("sales",marketplaceOverviewDao.sales(params3));
        res.put("refund",marketplaceOverviewDao.refund(params3));
        res.put("obligation",marketplaceOverviewDao.obligation(params));
        res.put("shipped",marketplaceOverviewDao.shipped(params));
        res.put("received",marketplaceOverviewDao.received(params));
        res.put("refundOrder",marketplaceOverviewDao.refundOrder(params));
        res.put("sellGoods",marketplaceOverviewDao.sellGoods(params3));
        res.put("inventory",marketplaceOverviewDao.inventory(params));
        String time2=sdf.format(new Date(time-day*2));
        String time3=sdf.format(new Date(time-day*3));
        String time4=sdf.format(new Date(time-day*4));
        Map<String,Object> params4 = new HashMap<>();
        params4.put("date",time2);
        params4.put("siteId",params.get("siteId"));
        Map<String,Object> params5 = new HashMap<>();
        params5.put("date",time3);
        params5.put("siteId",params.get("siteId"));
        Map<String,Object> params6 = new HashMap<>();
        params6.put("date",time4);
        params6.put("siteId",params.get("siteId"));
        List<Integer> list = new ArrayList<> ();
        list.add(marketplaceOverviewDao.ordersNumber(params6));
        list.add(marketplaceOverviewDao.ordersNumber(params5));
        list.add(marketplaceOverviewDao.ordersNumber(params4));
        list.add(marketplaceOverviewDao.ordersNumber(params2));
        list.add(marketplaceOverviewDao.ordersNumber(params3));
        res.put("ordersNumberImage",list);
        List<Integer> list2 = new ArrayList<> ();
        list2.add(marketplaceOverviewDao.sales(params6));
        list2.add(marketplaceOverviewDao.sales(params5));
        list2.add(marketplaceOverviewDao.sales(params4));
        list2.add(marketplaceOverviewDao.sales(params2));
        list2.add(marketplaceOverviewDao.sales(params3));
        res.put("salesImage",list2);
        return AjaxResult.success(res);
    }

}
